package com.draven.mall.controller;

import com.draven.mall.common.ApiResponse;
import com.draven.mall.entity.Merchant;
import com.draven.mall.entity.Product;
import com.draven.mall.service.MerchantService;
import com.draven.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户端商家相关控制器
 */
@RestController
@RequestMapping("/api/shop")
public class ShopController {

    @Autowired
    private MerchantService merchantService;
    
    @Autowired
    private ProductService productService;

    /**
     * 获取附近商家列表
     */
    @GetMapping("/nearby")
    public ApiResponse getNearbyShops(
            @RequestParam(required = false) Double latitude,
            @RequestParam(required = false) Double longitude,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        
        // 调用服务获取商家列表
        Map<String, Object> result = merchantService.getNearbyShops(latitude, longitude, categoryId, keyword, page, size);
        return ApiResponse.success(result);
    }

    /**
     * 获取商家详情
     */
    @GetMapping("/{id}")
    public ApiResponse getShopDetail(@PathVariable Long id) {
        Merchant merchant = merchantService.getById(id);
        if (merchant != null) {
            Map<String, Object> result = new HashMap<>();
            result.put("id", merchant.getId());
            result.put("shopName", merchant.getShopName());
            result.put("shopLogo", merchant.getShopLogo());
            result.put("address", merchant.getAddress());
            result.put("phone", merchant.getPhone());
            result.put("description", merchant.getDescription());
            result.put("rating", merchant.getRating());
            result.put("businessHours", merchant.getBusinessHours());
            result.put("minPrice", merchant.getMinPrice());
            result.put("deliveryFee", merchant.getDeliveryFee());
            
            // 在实际项目中还需要获取更多信息，如商家分类、标签等
            
            return ApiResponse.success(result);
        } else {
            return ApiResponse.error("商家不存在");
        }
    }
    
    /**
     * 获取推荐商家
     */
    @GetMapping("/recommended")
    public ApiResponse getRecommendedShops() {
        List<Merchant> recommendedShops = merchantService.getRecommendedShops();
        return ApiResponse.success(recommendedShops);
    }
    
    /**
     * 获取商家商品列表
     */
    @GetMapping("/{shopId}/products")
    public ApiResponse getShopProducts(
            @PathVariable Long shopId,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer size) {
        
        // 查询商家是否存在
        Merchant merchant = merchantService.getById(shopId);
        if (merchant == null) {
            return ApiResponse.error("商家不存在");
        }
        
        // 只查询上架的商品
        Integer status = 1;
        
        // 调用商品服务获取商品列表
        Map<String, Object> result = productService.getProductPage(shopId, categoryId, keyword, status, page, size);
        return ApiResponse.success(result);
    }
} 